#include "GPS_message.hpp"
#include "LCD_TFT_DRIVER.hpp"
UARTN_enum GPS_message_exchange::GPS_uart_type = UART_6;
GPS_message_to_division GPS_message_exchange::message_division;
bool GPS_message_exchange::read_start_flag;
bool GPS_message_exchange::read_a_peice_flag;
bool GPS_message_exchange::read_preparing_flag;
bool GPS_message_exchange::read_prepared_flag;
bool GPS_message_exchange::read_start_flag_sentence;
char GPS_message_exchange::str_head_check[7] = "$GNRMC";
double GPS_message_exchange::latitude_target;
double GPS_message_exchange::longitude_target;
uint8 char_in_temp = 0; //定义一个字符用于接收数字
char char_out_temp = 0; //定义一个输出字符
char chars_out_temp[20];

double pi = 3.1415926535897932384626;
double earth_radius = 6371000.000;                                              //地球半径
double earth_circle_radius = pi * earth_radius / double(180) / double(60);      // 地球周长过南北极
double earth_longitude_distance = pi * earth_radius / double(180) / double(60); //
bool GPS_message_exchange::is_message_available;

//定义发送的字符串

u8 GPS_message_exchange::init(void)
{
    int i = 0;
    uart_init(GPS_message_exchange::GPS_uart_type, 115200, UART6_TX_C06, UART6_RX_C07); //初始化GPS串口
    uart_rx_irq(GPS_message_exchange::GPS_uart_type, ENABLE);                           // 使能 UART6 的接收中断
    message_division.readed_number = 0;                                                 //初始化读取数字
    message_division.loop_count = 0;                                                    //初始化从0开始计数
    message_division.separator_count = 0;                                               //分割每段的个数
    message_division.check_loop_count = 0;                                              //分割总计数
    message_division.single_line_length = 0;                                            //分割字符总长
    message_division.line_text_number = 0;                                              //分割字符计数
    read_start_flag = 0;                                                                //是否开始读取
    read_a_peice_flag = 0;                                                              //是否开始字符分割
    read_prepared_flag = 0;                                                             //是否已准备好进入分割
    read_preparing_flag = 0;                                                            // 是否正在确认头是否正确
    is_message_available = 0;                                                           //设定是否可以读取
    latitude_target = 99999;                                                            //
    longitude_target = 99999;                                                           //目标经纬度写入一个极大数
    for (i = 0; i < 100; i++)
    {
        if (i < 20)
        {
            chars_out_temp[i] = 0;
        }
        GPS_message_exchange::message_division.message_origin[i] = 0;
    }
    // close_unused_message();
    // change_message_status();
    return 0;
}
void GPS_message_exchange::put_string(const u8 sender[], u8 message_length)
{
    int i = 0;
    while (i < message_length)
    {
        uart_putchar(UART_6, sender[i]);
        i++;
    }
}
//关闭无需使用的语句
void GPS_message_exchange::close_unused_message(void)
{
    u8 GSA_close[11] = {0xF1, 0xD9, 0x06, 0x01, 0x03, 0x00, 0xF0, 0x02, 0x00, 0xFC, 0x13};
    u8 GSV_close[11] = {0xF1, 0xD9, 0x06, 0x01, 0x03, 0x00, 0xF0, 0x04, 0x00, 0xFE, 0x17};
    put_string(GSA_close, 11);
    put_string(GSV_close, 11);
}

void GPS_message_exchange::change_message_status(void)
{
    u8 dual_frequency[12] = {0xF1, 0xD9, 0x06, 0x0C, 0x04, 0x00, 0x37, 0x82, 0x10, 0x04, 0xE3, 0x22};
    u8 all_system_support[12] = {0xF1, 0xD9, 0x06, 0x0C, 0x04, 0x00, 0x37, 0x82, 0x10, 0x04, 0xE3, 0x22};
    put_string(dual_frequency, 12);
    put_string(all_system_support, 12);
}
//获取经纬度
u8 GPS_message_exchange::get_message_latitude_longitude(double *latitude, double *longitude)
{
    if (is_message_available)
    {
        // LCD_TFT::replace_text(0, "message", 1);
        *latitude = message_division.message_latitude_double;
        *longitude = message_division.message_longitude_double;
        is_message_available = 0;
        return 1;
    }
    else
    {
        // LCD_TFT::replace_text(0, "not_available", 1);
        return -1;
    }
}
//计算距离目标的方式
u8 GPS_message_exchange::get_GPS_distance_meters_message(double *latitude_meters, double *longitude_meters)
{
    if (latitude_target < 5400 && longitude_target < 10800)
    {
        *latitude_meters = ((message_division.message_latitude_double - latitude_target) * earth_circle_radius);
        *longitude_meters = (message_division.message_longitude_double - longitude_target) * cos(latitude_target / double(60)) * earth_longitude_distance;
        // lcd_showfloat(0, 2, latitude_target / double(60), 5, 10);
        // lcd_showfloat(0, 3, (message_division.message_latitude_double - latitude_target) / double(60), 5, 10);
        return 1;
    }
    else
    {
        // lcd_showfloat(0, 2, latitude_target, 5, 10);
        // lcd_showfloat(0, 3, longitude_target, 5, 10);
        return -1;
    }
}
void GPS_message_exchange::set_GPS_target_message(double latitude0, double longitude0)
{
    if (latitude0 <= 5400.00)
    {
        latitude_target = latitude0;
    }
    else
    {
        // lcd_showfloat(0, 2, latitude0, 5, 5);
    }

    if (longitude0 < 10800.00)
    {
        longitude_target = longitude0;
    }
    else
    {
        // lcd_showfloat(0, 3, longitude0, 5, 5);
    }
}

int GPS_message_exchange::get_message_distancs_doubleDate(double latitude0, double longitude0, double latitude1, double longitude1, double *return_latitude_meters, double *return_longitude_meters)
{
    *return_latitude_meters = (sin(latitude0 / double(60)) - sin(latitude1 / double(60))) * earth_radius;
    *return_longitude_meters = (longitude0 - longitude1) / double(60) * cos(latitude0 / double(60)) * earth_longitude_distance;
    return 0;
}
// 整合信息到各种函数中，注意，此处每种语句改变时需要同时改变
void GPS_message_exchange::devide_message(void)
{
    int i = 0; //循环计数器
    char endptr;
    // LCD_TFT::replace_text(0, message_division.message_statements_separation[0], 1);
    message_division.message_time_UTC = strtod(&message_division.message_statements_separation[0][4], NULL);
    // message_division.message_time_UTC = atoi(&message_division.message_statements_separation[0][4]); //, NULL);
    if (message_division.message_statements_separation[1][0] == 'A')
    {
        message_division.message_latitude_double = strtod(&message_division.message_statements_separation[2][2], NULL); //从第三个字符开始取数

        message_division.message_latitude_double += (atoi(&message_division.message_statements_separation[2][0]) / 100) * 60; //加上前面的度数
        LCD_TFT::replace_text(2, message_division.message_statements_separation[2], 1);
        lcd_showfloat(0, 5, message_division.message_latitude_double, 5, 10);
        message_division.message_longitude_double = strtod(&message_division.message_statements_separation[4][3], NULL); //从第si个字符开始取数

        message_division.message_longitude_double += (atoi(&message_division.message_statements_separation[4][0]) / 100) * 60; //加上前面的度数
        LCD_TFT::replace_text(3, message_division.message_statements_separation[4], 1);
        lcd_showfloat(0, 6, message_division.message_longitude_double, 5, 10);
        is_message_available = 1; //确认可以读取
    }
    else
    {
        // LCD_TFT::replace_text(0, "Invalid_Data", 1);
    }
}
//检查字符串头信息
void GPS_message_exchange::check_start_string(void)
{
    // check
}
void GPS_message_exchange::show_gps_message(void)
{
    int i = 0;
    while (i < 9)
    {
        LCD_TFT::replace_text(i, message_division.message_statements_separation[i], 1);
        i++;
    }
}
//检查信息
void GPS_message_exchange::message_check(void)
{
    char check_character = (char)0;
    if (read_start_flag_sentence)
    {
        // lcd_showint16(0, 3, GPS_message_exchange::message_division.separator_count);
        // message_division.message_statements_start[message_division.check_loop_count] = message_division.message_origin[message_division.readed_number];
        check_character = message_division.message_statements_start[message_division.readed_number];

        if (check_character == '\r') //判断到达字符串结尾
        {
            GPS_message_exchange::devide_message();
            lcd_showfloat(0, 1, message_division.message_time_UTC, 5, 5);
            //  show_gps_message();
            read_start_flag_sentence = 0;
            read_prepared_flag = 0;
            read_preparing_flag = 0;
            message_division.line_text_number = 0;
            message_division.separator_count = 0;
        }
        else if (message_division.separator_count >= 17 || message_division.line_text_number >= 16) //数据超出限制，读取失败
        {
            LCD_TFT::replace_text(1, "toolong", 1);
            read_start_flag_sentence = 0;
            read_prepared_flag = 0;
            read_preparing_flag = 0;               //直接退出两个循环
            message_division.line_text_number = 0; //
            message_division.separator_count = 0;  //直接双重归零
        }
        else if (check_character == ',') //判断语句分隔符
        {
            printf("%d", message_division.readed_number);
            message_division.message_statements_separation[message_division.separator_count][message_division.line_text_number] = '\0'; //
            message_division.line_text_number = 0;
            message_division.separator_count++; //分割字符串+1
        }
        else
        {
            message_division.message_statements_separation[message_division.separator_count][message_division.line_text_number] = check_character;
            message_division.line_text_number++; //指向下一个字符点
        }
        // }
        message_division.check_loop_count++; //指向下一个写入点
    }
    else
    {
        message_division.separator_count = 0;  //分割每段的个数回零
        message_division.check_loop_count = 0; //分割总计数回零
        message_division.line_text_number = 0; //分割字符计数回零//始终不开始写入
    }
}
//获取字符
void GPS_message_exchange::get_message(void)
{
    int length_template = message_division.single_line_length;
    // printf("g\n");
    message_division.readed_number = 0; //清零输入
    //如果有写入内容的话
    if (length_template != 0)
    {
        printf("on_gps_message_reading\n");

        while (message_division.readed_number < length_template)
        {
            GPS_message_exchange::message_division.message_statements_start[message_division.readed_number] = GPS_message_exchange::message_division.message_origin[GPS_message_exchange::message_division.readed_number + 1];
            message_division.readed_number++;
        }
        // LCD_TFT::replace_text(0, GPS_message_exchange::message_division.message_statements_start, 1);
        message_division.message_statements_start[message_division.readed_number] = '\0';
        message_division.readed_number = 0; //把坐标移动到第一个
        read_start_flag_sentence = 1;
        while (message_division.readed_number < length_template)
        {
            // lcd_showint16(0, 2, GPS_message_exchange::message_division.readed_number);
            GPS_message_exchange::message_check();
            message_division.readed_number++;
        }
        message_division.single_line_length = 0; //语句长度清零
        message_division.readed_number = 0;      //把坐标移动到第一个
    }

    // return 0;
}
void _used_for_c_()
{
    int i = 0;
    // GPS_message_exchange::message_division.loop_count = 0;
    //  UART6 中断标志置位
    // GPS_message_exchange::message_division.message_origin[GPS_message_exchange::message_division.loop_count] = char_in_temp;
    if (GPS_message_exchange::read_start_flag)
    {
        // printf("on_gps_message_reading\n");
        if (GPS_message_exchange::message_division.loop_count < 100 && char_in_temp != '\n') //判断是否到达语句结尾
        {
            GPS_message_exchange::message_division.loop_count++;
            GPS_message_exchange::message_division.message_origin[GPS_message_exchange::message_division.loop_count] = char_in_temp;
        }
        else
        { //语句尾结束并退出
            GPS_message_exchange::message_division.single_line_length = GPS_message_exchange::message_division.loop_count;
            GPS_message_exchange::read_start_flag = 0;
            GPS_message_exchange::message_division.loop_count = 0;
        }
    }
    else if (GPS_message_exchange::str_head_check[GPS_message_exchange::message_division.loop_count] == char_in_temp) //判断是否是行开头
    {
        GPS_message_exchange::message_division.loop_count++;
    }
    else if (GPS_message_exchange::str_head_check[GPS_message_exchange::message_division.loop_count] == '\0') //判断判断字符结尾
    {
        GPS_message_exchange::read_start_flag = 1;
        GPS_message_exchange::message_division.loop_count = 0; //清零
    }
    else
    { //如果不符合要求
        GPS_message_exchange::read_start_flag = 0;
        GPS_message_exchange::message_division.loop_count = 0;
    }
}
extern "C"
{
    void uart6_interrupt_handler(void) // 这个函数在 isr.c 的 UART6_IRQHandler 中调用
    {
        // uart6_interrupt_flag = true;
        uart_getchar(UART_6, &char_in_temp); // 读取数据
        _used_for_c_();
        // uart_putchar(UART_1, char_in_temp);
    }
}